package com.tf_cut.backend.service.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.tf_cut.backend.common.pojo.Result;
import com.tf_cut.backend.service.service.ServiceInfoService;
import com.tf_cut.backend.service.service.ServicePersonalIncomeTaxSetingService;
import com.tf_cut.backend.service.vo.ServiceInfoVo;
import com.tf_cut.backend.service.vo.ServicePersonalIncomeTaxSetingVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/backend/service/")
@Slf4j
public class ServiceController {
	private Logger logger = LogManager.getLogger(ServiceController.class);

	@Autowired
	private ServiceInfoService serviceInfoService;
	@Autowired
	private ServicePersonalIncomeTaxSetingService servicePersonalIncomeTaxSetingService;


	@RequestMapping("ServicePersonalIncomeTaxSeting/queryList")
	public String ServicePersonalIncomeTaxSetingQuery(String json) {
		ServicePersonalIncomeTaxSetingVo avo = JSON.parseObject(json, ServicePersonalIncomeTaxSetingVo.class);
		return JSON.toJSONString(servicePersonalIncomeTaxSetingService.queryByServiceId(avo));
	}


	@RequestMapping("ServicePersonalIncomeTaxSeting/insert")
	public String insertServicePersonalIncomeTaxSeting(String json) {
		Result<ServicePersonalIncomeTaxSetingVo> result;
		ServicePersonalIncomeTaxSetingVo avo = JSON.parseObject(json, ServicePersonalIncomeTaxSetingVo.class);
		try {
			result = servicePersonalIncomeTaxSetingService.save(avo);
		} catch(Exception e) {
			log.error("insertServiceInfo error:{}",e.getMessage(),e);
			result = Result.of(avo);
			result.setMsg(e.getMessage());
			result.setFlag(false);
		}
		return JSON.toJSONString(result);
	}


	@RequestMapping("ServicePersonalIncomeTaxSeting/updateInfo")
	public String ServicePersonalIncomeTaxSetingUpdateInfo( String json) {
		ServicePersonalIncomeTaxSetingVo svo = JSON.parseObject(json, ServicePersonalIncomeTaxSetingVo.class);
		Result<ServicePersonalIncomeTaxSetingVo> result = servicePersonalIncomeTaxSetingService.save(svo);
		return JSON.toJSONString(result.getData());
	}


	@RequestMapping("ServicePersonalIncomeTaxSeting/delete")
	public String ServicePersonalIncomeTaxSetingDelete( String json) {
		ServicePersonalIncomeTaxSetingVo svo = JSON.parseObject(json, ServicePersonalIncomeTaxSetingVo.class);
		Result<String> result = servicePersonalIncomeTaxSetingService.delete(svo.getId());
		return JSON.toJSONString(result.getData());
	}

	@RequestMapping("insert")
    public String insertServiceInfo(String json) {
		Result<ServiceInfoVo> result;
		ServiceInfoVo avo = JSON.parseObject(json, ServiceInfoVo.class);
		try {
			result = serviceInfoService.insertServiceInfo(avo);
		} catch(Exception e) {
			log.error("insertServiceInfo error:{}",e.getMessage(),e);
			result = Result.of(avo);
			result.setMsg(e.getMessage());
			result.setFlag(false);
		}
        return JSON.toJSONString(result);
    }
	
	@RequestMapping("updateInfo")
    public String updateInfo( String json) {
		ServiceInfoVo svo = JSON.parseObject(json, ServiceInfoVo.class);
		Result<ServiceInfoVo> result = serviceInfoService.save(svo);
        return JSON.toJSONString(result.getData());
    }
	
	@RequestMapping("get")
    public String get( String id) {
		Result<ServiceInfoVo> result = serviceInfoService.get(id);
		return JSON.toJSONString(result.getData(),SerializerFeature.WriteDateUseDateFormat);
    }
	
	@RequestMapping("operation")
    public String operation(String id, String state) {
		Result<Boolean> result = Result.of(true);
		result.setFlag(true);
		try {
			ServiceInfoVo avo = serviceInfoService.get(id).getData();
			avo.setServiceState(state);
			serviceInfoService.save(avo);
		} catch(Exception e) {
			log.error("service operation error:{}",e.getMessage(),e);
			result = Result.of(false);
			result.setFlag(false);
		}
		return JSON.toJSONString(result);
    }
	
	@RequestMapping("findListService")
	public String findListService(String json) {
		logger.info("[findListService]:json : " + json);
        ServiceInfoVo avo = JSON.parseObject(json, ServiceInfoVo.class);
		return JSON.toJSONString(serviceInfoService.queryServiceInfoList(avo),SerializerFeature.WriteDateUseDateFormat);
	}

	@RequestMapping("findListServiceNoPage")
	public String findListServiceNoPage(String json) {
		ServiceInfoVo avo = JSON.parseObject(json, ServiceInfoVo.class);
		return JSON.toJSONString(serviceInfoService.list(avo).getData());
	}
	
	
    /**
     * 查询人资入驻结果
     */
    @PostMapping("registerQuery")
    public void registerQuery(String serviceId){
    	serviceInfoService.registerQuery(serviceId);
    }
    
    /**
     * 人资公司入驻
     */
    @PostMapping("insertRegister")
    public String insertRegister(String serviceId){
		return JSON.toJSONString(serviceInfoService.register(serviceId));
    }
}
